package com.yy.tankgame;
import java.awt.Color;
import java.awt.Graphics;
import java.util.List;
import java.awt.Rectangle;

/**
 * if(good) setcolor red
 * else setcolor black
 *@author Yun
 *@date 2014-9-8
 *@version 0.9
 */

public class Missile {
	int x,y;
	public static final int XSPEED = 20;
	public static final int YSPEED = 20;
	
	public static final int WIDTH = 10;
	public static final int HEIGTH = 10;
	
	Direction dir;
	
	private boolean Live = true;
	private TankClient tc;
	
	private boolean good;	
	
	public boolean isLive() {
		return Live;
	}
	public Missile(int x ,int y, Direction dir){
		this.x = x;
		this.y = y;
		this.dir =dir;
	}
	
	public Missile(int x ,int y, boolean good, Direction dir, TankClient tc){
		this(x, y, dir);
		this.good = good;
		this.tc = tc;
		
	}
	public void draw(Graphics g){
		if(!Live){
			tc.missiles.remove(this);	
			return;
		}
		Color c = g.getColor();
		if(good){
			g.setColor(Color.red);
		}else
			g.setColor(Color.lightGray);
		g.fillOval(x, y, WIDTH, HEIGTH);
		g.setColor(c);
		
		move();
	}
	
	void move(){
		
		switch (dir){
		case L:
			x -=XSPEED;
			break;
		case LU:
			x -=XSPEED;
			y -=YSPEED;
			break;
		case U:
			y -=YSPEED;
			break;
		case RU:
			x +=XSPEED;
			y -=YSPEED;
			break;
		case R:
			x +=XSPEED;
			break;
		case RD:
			x +=XSPEED;
			y +=YSPEED;
			break;
		case D:
			y +=XSPEED;
			break;
		case LD:
			x -=XSPEED;
			y +=YSPEED;
			break;
		default:
			break;
			
		}
		
		if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT){
			Live = false;
					
		}
	}
	public Rectangle getRect(){
		return new Rectangle(x,y,WIDTH,HEIGTH);
	}

	public boolean hitTank(Tank t){
		if(this.Live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()){
			if(t.isGood()){
				t.setLife(t.getLife() - 30);
				if(t.getLife() <= 0) t.setLive(false);
			}else{
				t.setLive(false);
			}			
			
			this.Live = false;
			Explode e = new Explode(x, y, tc);
			tc.explode.add(e);
			return true;
		}
		return false;
	}
	
	public boolean hitTanks(List<Tank> t){
		for(int i = 0; i < t.size(); i++){
			if(hitTank(t.get(i)))
					return true;
		}
		
		return false;
	}
	
	public boolean hitWall(Wall w){
		if(this.Live && this.getRect().intersects(w.getRect())){
			this.Live = false;
			return true;
			
		}
		return false;
	}
}
